//
//  RoomDetailsViewController.m
//  mUBB
//
//  Created by Cosmin Stirbu on 3/13/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "RoomDetailsViewController.h"
#import "RoomsManager.h"
#import "UIViewAdditions.h"
#import "WSError.h"
#import "Constants.h"
#import "BuildingMapViewController.h"
#import "RoomScheduleViewController.h"

#define VERTICAL_SPACE (IS_IPAD() ? 40 : 20)

@interface RoomDetailsViewController ()

@end

@implementation RoomDetailsViewController

@synthesize buildingButton, buildingInformationLabel, roomInformationLabel, roomScheduleButton, roomInformationHeader;

- (id)initWithNibName:(NSString *)nibNameOrNil room:(Room *)room
{
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {
        // Custom initialization
        _room = room;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // Self title
    self.title = _room.name;
    
    // Get Room Details
    [Utils showLoadingOn:self];
    [[RoomsManager instance] getDetailsForRoom:_room delegate:self];
}

- (void)setUpTheUI {
    // Self title
    self.title = _room.name;
    
    [roomInformationLabel setHidden:NO];
    [roomScheduleButton setHidden:NO];
    [buildingInformationLabel setHidden:NO];
    [buildingButton setHidden:NO];
    [roomInformationHeader setHidden:NO];
    
    roomInformationHeader.text = NSLocalizedString(@"ROOM_INFORMATION", nil);
    [roomInformationHeader setTextColor:[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"welcome_screen_labels_color"]]];
    
    if (_room.hasProjector) {
        roomInformationLabel.text = [NSString stringWithFormat:NSLocalizedString(@"ROOM_HAS_PROJECTOR_LABEL", nil), _room.name, _room.name, _room.numberOfPlaces];
    } else {
        roomInformationLabel.text = [NSString stringWithFormat:NSLocalizedString(@"ROOM_HASNT_PROJECTOR_LABEL", nil), _room.name, _room.name, _room.numberOfPlaces];
    }
    // Adjust roomInformationLabelSize
    CGSize size = [roomInformationLabel.text sizeWithFont:roomInformationLabel.font constrainedToSize:CGSizeMake(roomInformationLabel.width, 9999) lineBreakMode:roomInformationLabel.lineBreakMode];
    roomInformationLabel.height = size.height;
    [roomInformationLabel setTextColor:[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"welcome_screen_labels_color"]]];

    roomScheduleButton.top = roomInformationLabel.bottom + VERTICAL_SPACE;
    buildingButton.top = roomScheduleButton.bottom + 2 * VERTICAL_SPACE;
    
    [Utils customizeButton:buildingButton withBgImageKey:@"button_image" imageKey:nil textKey:nil textColorKey:@"welcome_screen_buttons_text_color"];
    [Utils customizeButton:roomScheduleButton withBgImageKey:@"button_image" imageKey:nil textKey:@"ROOM_SCHEDULE" textColorKey:@"welcome_screen_buttons_text_color"];
    [buildingButton setTitle:_room.building.name forState:UIControlStateNormal];
    
    [buildingInformationLabel setText:_room.building.street];
    [buildingInformationLabel setTextColor:[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:@"welcome_screen_labels_color"]]];
    size = [buildingInformationLabel.text sizeWithFont:buildingInformationLabel.font constrainedToSize:CGSizeMake(buildingInformationLabel.width, 9999) lineBreakMode:buildingInformationLabel.lineBreakMode];
    buildingInformationLabel.height = size.height;
    buildingInformationLabel.top = buildingButton.bottom + VERTICAL_SPACE;
    
}

#pragma mark - UI Interaction

- (IBAction)buildingButtonTapped:(id)sender {
    BuildingMapViewController *buildingMapViewController = [[BuildingMapViewController alloc] initWithNibName:@"BuildingMapViewController" building:_room.building];
    [self.navigationController pushViewController:buildingMapViewController animated:YES];
}

- (IBAction)roomsScheduleButtonTapped:(id)sender {
    RoomScheduleViewController *roomSchedule = [[RoomScheduleViewController alloc] initWithNibName:@"RoomScheduleViewController" room:_room];
    roomSchedule.title = NSLocalizedString(@"ROOM_SCHEDULE", nil);
    [self.navigationController pushViewController:roomSchedule animated:YES];
}



#pragma mark - RoomsProtocol Delegate

- (void)didReceiveRoomDetailsWithSuccess:(id)object forTicket:(Ticket *)ticket {
    [Utils hideLoadingOn:self];
    if (!_isOnScreen) {
        return;
    }
    [self setUpTheUI];
}

- (void)didReceiveRoomDetailsWithFailure:(id)object forTicket:(Ticket *)ticket {
    [Utils hideLoadingOn:self];
    if (!_isOnScreen) {
        return;
    }
    WSError *error = (WSError *)object;
    if (error.errorCode == StatusCodeConnectionError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.errorMessage delegate:self cancelButtonTitle:NSLocalizedString(@"ALERT_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"ALERT_RETRY", nil), nil];
        [alertView show];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error.errorMessage delegate:nil cancelButtonTitle:NSLocalizedString(@"ALERT_OK", nil) otherButtonTitles: nil];
        [alertView show];
    }
}

#pragma mark - UIAlertViewDelegate Methods

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        // Get Study Groups
        [Utils showLoadingOn:self];
        [[RoomsManager instance] getDetailsForRoom:_room delegate:self];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setRoomInformationLabel:nil];
    [self setRoomScheduleButton:nil];
    [self setBuildingButton:nil];
    [self setBuildingInformationLabel:nil];
    [self setRoomInformationHeader:nil];
    [super viewDidUnload];
}
@end
